package com.jiutong.meipengdai.views.dialog;

import android.annotation.SuppressLint;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseApplicationLike;
import com.jiutong.meipengdai.common.Constants;
import com.jiutong.meipengdai.entity.CityListRespon;
import com.jiutong.meipengdai.http.model.requests.CommonRequest;
import com.jiutong.meipengdai.http.subscribers.ProgressSubscriber;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.IOUtil;
import com.jiutong.meipengdai.views.wheelview.OnWheelChangedListener;
import com.jiutong.meipengdai.views.wheelview.WheelView;
import com.jiutong.meipengdai.views.wheelview.adapter.ArrayWheelAdapter;

import java.util.HashMap;
import java.util.List;

/**
 * Created by suxi on 2017/6/8.
 */

@SuppressLint("ValidFragment")
public class DoNetWorkCityPicker extends DialogFragment implements OnWheelChangedListener, View.OnClickListener {

    private View mContainer;
    private View btnOk;
    private View btnCancel;
    private WheelView wvCity;
    private WheelView wvArea;

    private OnDialogClickListener mListener;
    private List<CityListRespon.CityBeanc> cityList;

    private List<CityListRespon.CityBeanc.AreaBean> areaList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //去掉dialog的标题，需要在setContentView()之前
        this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的位置在底部
        lp.gravity = Gravity.BOTTOM;
        //设置dialog的动画
        lp.windowAnimations = R.style.dialog_animation;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
        mContainer = inflater.inflate(R.layout.dialog_customer_city_picker, null);

        initView();
        initEvent();
//        getCityList();

        CityListRespon nativeBean = (CityListRespon) IOUtil.getObject(getContext(), Constants.CITYLIST);
        cityList = nativeBean.getData();
        updateCity();
        return mContainer;
    }

    private void initView() {
        wvCity = (WheelView) mContainer.findViewById(R.id.wv_city);
        wvArea = (WheelView) mContainer.findViewById(R.id.wv_area);
        btnOk = mContainer.findViewById(R.id.btn_ok);
        btnCancel = mContainer.findViewById(R.id.btn_cancel);

    }

    private void initEvent() {
        wvCity.addChangingListener(this);
        wvArea.addChangingListener(this);
        btnOk.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    public void setOnDialogClickListener(OnDialogClickListener listener) {
        this.mListener = listener;
    }

    private void updateCity() {

        String[] cityArray = new String[cityList.size()];
        for (int i = 0; i < cityList.size(); i++) {
            cityArray[i] = cityList.get(i).getAreaName();
        }
        wvCity.setViewAdapter(new ArrayWheelAdapter<String>(BaseApplicationLike.getInstance().mContext, cityArray));
        wvCity.setCurrentItem(0);
        updateArea();
    }

    private void updateArea() {

        int index = wvCity.getCurrentItem();
        areaList = cityList.get(index).getAreaList();

        String[] areaArray = new String[areaList.size()];

        for (int i = 0; i < areaList.size(); i++) {
            areaArray[i] = areaList.get(i).getAreaName();
        }

        wvArea.setViewAdapter(new ArrayWheelAdapter<String>(BaseApplicationLike.getInstance().mContext, areaArray));
        wvArea.setCurrentItem(0);
    }


    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == wvCity) {
            updateArea();
        } else if (wheel == wvArea) {
//            currentArea = areaList.get(newValue).getAreaName();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                int currentCityIndex = wvCity.getCurrentItem();
                String currentCity = cityList.get(currentCityIndex).getAreaName();
                int currentAreaIndex = wvArea.getCurrentItem();
                String currentArea = areaList.get(currentAreaIndex).getAreaName();
                int code = areaList.get(currentAreaIndex).getId();
                mListener.onBtnClick(currentCity, currentArea, code + "");

                dismiss();
                break;

            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }

    public interface OnDialogClickListener {

        void onBtnClick(String city, String area, String code);
    }


    private void getCityList() {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<CityListRespon>() {
            @Override
            public void onNext(CityListRespon subjects) {
                cityList = subjects.getData();
                updateCity();
            }

            @Override
            public void onError(Throwable e) {

            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.getAreaDate(new ProgressSubscriber(onNextListener, getContext()), fieldsMap);
    }
}
